package login

import (
	"encoding/json"
	"os"
	"path/filepath"
	"sync"

	"gitee.com/tomatomeatman/golang-repository/bricks3/utils/app/moduleutil"
	Log "github.com/cihub/seelog"
)

var (
	loginBusinessInfo     *LoginBusinessInfo
	loginBusinessInfoOnce sync.Once
)

type LoginBusinessInfo struct {
	DbName     string `json:"dbName"`      //所用库
	TableName  string `json:"tableName"`   //所用表名",
	Id         string `json:"sId"`         //记录编号字段名",
	Name       string `json:"sName"`       //用户名字段名",
	No         string `json:"sNo"`         //用户唯一标识字段名",
	Pass       string `json:"sPass"`       //用户密码字段名",
	Type       string `json:"sType"`       //类型",
	State      string `json:"iState"`      //账户状态字段名",
	StateValue string `json:"iStateValue"` //有效状态值",
}

/**
 * 登录缓存对象
 * 非数据库实体
 */
type LoginUser struct {
	Id       string            `json:"sId" gorm:"column:sId; type:varchar; NOT NULL; primary_key; defaultData:''; comment:表编号"`
	Name     string            `json:"sName" gorm:"column:sName; type:varchar; NOT NULL; defaultData:''; comment:名称"`
	No       string            `json:"sNo" gorm:"column:sNo; type:varchar; NOT NULL; defaultData:''; comment:标识(组合唯一)"`
	Pass     string            `json:"sPass" gorm:"column:sPass; type:varchar; NOT NULL; defaultData:''; comment:密码"`
	Type     string            `json:"sType" gorm:"column:sType; type:varchar(50); NOT NULL; defaultData:管理员; comment:类型"`
	State    int               `json:"iState" gorm:"column:iState; type:int; NOT NULL; defaultData:1; comment:状态(枚举,1:启用;2:禁用)"`
	LastDate int64             `json:"dLastDate"`         //最后访问时间
	Token    string            `json:"token"`             //分配的Token
	Device   int               `json:"device"`            //设备类型,1:PC,2:手机,3:平板,4.....
	Attached map[string]string `json:"attached" gorm:"-"` //与登录相关的附属信息
}

func GetLoginBusinessInfo() *LoginBusinessInfo {
	loginBusinessInfoOnce.Do(func() {
		root := ""
		exePath, err := os.Executable()
		if err != nil {
			root = "."
		}

		root, _ = filepath.EvalSymlinks(filepath.Dir(exePath))

		business := filepath.Join(root, "config/LoginBusiness.json")

		if _, err := os.Stat(business); os.IsNotExist(err) {
			Log.Error("业务配置文件不存在:", "config/LoginBusiness.json")
			return
		}

		data, err := os.ReadFile(business)
		if err != nil {
			Log.Error("读取配置文件发生异常:", err)
			return
		}

		temp := LoginBusinessInfo{}
		err = json.Unmarshal(data, &temp)
		if err != nil {
			Log.Error("配置信息转实体发生异常:", err)
			return
		}

		loginBusinessInfo = &temp
	})

	if loginBusinessInfo == nil {
		loginBusinessInfo = &LoginBusinessInfo{
			DbName:     moduleutil.GetMainDb(),
			TableName:  "LoginUser",
			Id:         "sId",
			Name:       "sName",
			No:         "sNo",
			Pass:       "sPass",
			State:      "iState",
			Type:       "sType",
			StateValue: "1",
		}
	}

	return loginBusinessInfo
}

/**
 * 复制
 * @return
 */
func (lu *LoginUser) Clone() *LoginUser {
	result := LoginUser{}
	result.Id = lu.Id
	result.Name = lu.Name
	result.No = lu.No
	result.Pass = lu.Pass
	result.State = lu.State
	result.Type = lu.Type
	result.LastDate = lu.LastDate
	result.Token = lu.Token
	result.Device = lu.Device
	result.Attached = lu.Attached

	if len(lu.Attached) > 0 {
		result.Attached = map[string]string{}
		for key, val := range lu.Attached {
			result.Attached[key] = val
		}
	}

	return &result
}
